"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/14 11:18
@Description : 
   单例模式   实现方式3   __new__(cls,*args,**kwargs)  魔术方法
"""


class Person:
    cache_instance = {}

    def __init__(self, name):
        print('--------__init__----------')
        self.name = name

    def __new__(cls, *args, **kwargs):
        print('----------__new__----------------')
        if cls not in cls.cache_instance:
            # cls.cache_instance[cls] = cls()  # RecursionError: maximum recursion depth exceeded
            cls.cache_instance[cls] = super().__new__(cls)  # 通过父类的__new__ 方法实现对象的实例化  避免RecursionError
        return cls.cache_instance[cls]


p1 = Person('tom')
print(id(p1), p1.name)
p2 = Person('jack')
print(id(p2), p2.name)
